#include <iostream>
#include <stdexcept> // 用于异常处理

class MyArray {
private:
    int* m_data;     // 存储数组数据的指针
    size_t m_size;   // 数组大小

public:
    // 1. 构造函数
    // 默认构造函数（创建大小为10的数组）
    MyArray() : m_size(10) {
        m_data = new int[m_size];
        for (size_t i = 0; i < m_size; ++i) {
            m_data[i] = 0; // 初始化为0
        }
    }

    // 带大小的构造函数
    MyArray(size_t size) : m_size(size) {
        if (size == 0) {
            throw std::invalid_argument("Array size cannot be zero");
        }
        m_data = new int[m_size];
        for (size_t i = 0; i < m_size; ++i) {
            m_data[i] = 0; // 初始化为0
        }
    }

    // 2. 拷贝构造函数（深拷贝）
    MyArray(const MyArray& other) : m_size(other.m_size) {
        m_data = new int[m_size];
        for (size_t i = 0; i < m_size; ++i) {
            m_data[i] = other.m_data[i];
        }
    }

    // 3. 析构函数
    ~MyArray() {
        delete[] m_data;
    }

    // 4. 赋值运算符（深拷贝）
    MyArray& operator=(const MyArray& other) {
        if (this != &other) { // 防止自赋值
            delete[] m_data;   // 释放原有内存
            
            m_size = other.m_size;
            m_data = new int[m_size];
            
            for (size_t i = 0; i < m_size; ++i) {
                m_data[i] = other.m_data[i];
            }
        }
        return *this;
    }

    // 5. 获取数组大小
    size_t size() const {
        return m_size;
    }

    // 6. 元素访问（可读写）
    int& operator[](size_t index) {
        if (index >= m_size) {
            throw std::out_of_range("Index out of bounds");
        }
        return m_data[index];
    }

    // 7. 元素访问（只读）
    const int& operator[](size_t index) const {
        if (index >= m_size) {
            throw std::out_of_range("Index out of bounds");
        }
        return m_data[index];
    }

    // 8. 遍历数组（使用函数指针）
    void forEach(void (*func)(int)) {
        for (size_t i = 0; i < m_size; ++i) {
            func(m_data[i]);
        }
    }

    // 9. 打印数组内容
    void print() const {
        std::cout << "[";
        for (size_t i = 0; i < m_size; ++i) {
            std::cout << m_data[i];
            if (i != m_size - 1) {
                std::cout << ", ";
            }
        }
        std::cout << "]" << std::endl;
    }
};

// 测试代码
int main() {
    // 测试默认构造函数
    MyArray arr1;
    std::cout << "arr1 (default): ";
    arr1.print();

    // 测试带大小的构造函数
    MyArray arr2(5);
    std::cout << "arr2 (size=5): ";
    arr2.print();

    // 测试拷贝构造函数
    MyArray arr3;
    for (size_t i = 0; i < arr3.size(); ++i) {
        arr3[i] = static_cast<int>(i + 1); // 手动初始化
    }
    std::cout << "arr3 (manually initialized): ";
    arr3.print();

    MyArray arr4 = arr3;
    std::cout << "arr4 (copy of arr3): ";
    arr4.print();

    // 测试赋值运算符
    MyArray arr5;
    arr5 = arr3;
    std::cout << "arr5 (assigned from arr3): ";
    arr5.print();

    // 测试元素访问
    arr5[0] = 10;
    arr5[1] = 20;
    std::cout << "After modification, arr5: ";
    arr5.print();

    // 测试越界访问
    try {
        std::cout << "Attempting to access arr5[10]: ";
        std::cout << arr5[10] << std::endl;
    } catch (const std::out_of_range& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }

    // 测试遍历功能
    std::cout << "arr5 elements (using forEach): ";
    arr5.forEach([](int val) {
        std::cout << val << " ";
    });
    std::cout << std::endl;

    return 0;
}